package org.eclipse.football.web;

import java.io.File;
import java.io.IOException;

public class ReadData {

	public static void ReadData() throws IOException {
		File weekRush = null;
		File weekPass = null;
		File weekKick = null;
		File weekRecv = null;
		boolean exists = false;
		//TODO: change the 3 to the actual number of weeks in the season
		for (Integer x = 1; x < 18; x++) {
			weekRush = new File("rushing" + x + "clean.txt");
			weekPass = new File("passing" + x + "clean.txt");
			weekKick = new File("kicking" + x + "clean.txt");
			weekRecv = new File("receiving" + x + "clean.txt");
//			System.out.println("rush length = " + weekRush.length());
//			System.out.println("kick length = " + weekKick.length());
			if ((weekRush.length() == 0) && (weekPass.length() == 0)
					&& (weekKick.length() == 0) && (weekRecv.length() == 0)) {
//				System.out.println("rush length = " + weekRush.length());
//				System.out.println("kick length = " + weekKick.length());
//				int prevInt = x-1;
//				File weekRushPrev = new File("rushing" + prevInt + "clean.txt");
//				File weekPassPrev = new File("passing" + prevInt + "clean.txt");
//				File weekKickPrev = new File("kicking" + prevInt + "clean.txt");
//				File weekRecvPrev = new File("receiving" + prevInt + "clean.txt");
//				if ((weekRushPrev.length() == 0) && (weekPassPrev.length() == 0)
//						&& (weekKickPrev.length() == 0) && (weekRecvPrev.length() == 0)){
//					x = 17;
//				}
				exists = false;
			}else{
				exists = true;
			}
			
			if (exists) {
//				System.out.println("File exists and is greater than 0");
				ReadURL(x.toString());
				
			} else {
//				System.out.println("file does not exist or is = 0");
				ReadAndWrite(x.toString());
			}
		}
		GetDataFromURL.getDefData();
		ReadDataFromURL.readDef();
	}

	public static void ReadURL(String week) throws IOException {
		ReadDataFromURL.readFile("passing", week);
		ReadDataFromURL.readFile("rushing", week);
		ReadDataFromURL.readFile("receiving", week);
		ReadDataFromURL.readFile("kicking", week);
		//
	}

	public static void ReadAndWrite(String week) throws IOException {
		GetDataFromURL.getData(week, "passing");
		GetDataFromURL.getData(week, "rushing");
		GetDataFromURL.getData(week, "receiving");
		GetDataFromURL.getData(week, "kicking");
		ReadURL(week);
	}
}
